import random
from django.shortcuts import render
from rest_framework.response import Response
from user import seruser, models
from rest_framework.views import APIView
from utils.tooljwt import tooljwt
from utils.authentication import Authen
from rest_framework import status
from utils.sendmes import send_message
import json
from utils.myredis import myredis
# Create your views here.


class User(APIView):

    def post(self, request):
        ser = seruser.SerUser(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({'msg': 'ok', 'data': ser.data, 'code': status.HTTP_201_CREATED})
        else:
            return Response({'msg': '注册失败', 'data': ser.errors, 'code': status.HTTP_400_BAD_REQUEST})


class Login(APIView):
    def post(self, request):
        # return Response({'token': tooljwt.gettoken({'id': 1})})
        username = request.data.get('username')
        password = request.data.get('password')
        data = models.User.objects.filter(username=username).first()
        if data:
            if data.password == password:
                token = tooljwt.gettoken({'id': data.id, 'username': data.username})
                return Response({'msg': 'ok', 'data': {'id': 1, 'username': data.username, 'token': token}})
            else:
                return Response({'msg': '密码错误'})
        else:
            return Response({'msg': '用户名错误'})


class Center(APIView):
    authentication_classes = (Authen,)

    def get(self, request):
        return Response({'msg': '会员中心'})


class SendCode(APIView):
    # 获取前端传递的手机号
    def post(self, request):
        phone = request.data.get('phone')
        # 生成随机验证码
        code = str(random.randint(1000, 9999))
        res = json.loads(send_message(phone, code, 1440))
        if res["statusCode"] == "000000":
            myredis.set_time_str(phone, 60*1440, code)
            return Response({'msg': '验证码发送成功'})
        else:
            return Response({'msg': res['statusMsg']})


# class GetCode(APIView):
#     def post(self, request):
#         phone = request.data.get('phone')
#         code = request.data.get('code')
#         # 验证手机号是否正确
#         data = models.User.objects.filter(phone=phone).first()
#         if data:
#             my_code = myredis.get_str(phone)
#             if code == my_code:
#                 # 手动生成token
#                 token = tooljwt.gettoken({'id': data.id})
#                 return Response({'msg': '登陆成功', 'data': {'id': data.id, 'token': token}})
#             else:
#                 return Response({'msg': '验证码不正确'})
#         else:
#             return Response('手机号错误')


class GetCode(APIView):
    def post(self, request):
        phone = request.data.get('phone')
        code = request.data.get('code')
        my_code = myredis.get_str(phone)
        if my_code == code:
            data = models.User.objects.filter(phone=phone).first()
            if not data:
                data = models.User.objects.create(phone=phone, username=phone, password="111111", email=phone + "@qq.com")
            token = tooljwt.gettoken({'id': data.id})
            return Response({'msg': '登录成功', 'data': {'id': data.id, 'token': token}})
        else:
            return Response({'msg': '验证码错误'})

